package stm

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	nhttp "net/http"
	"wzgames/internal/admin/adc"
	"wzgames/internal/admin/adsv/stmsv"
	"wzgames/internal/admin/api/mid"
	"wzgames/internal/admin/api/wzabind"
	"wzgames/internal/admin/model"
	"wzgames/internal/response"
	"wzgames/pkg/logger"
)

var userBetsFn = func() (string, func(c *gin.Context)) {
	uri := fmt.Sprintf("/user/bets")
	return uri, func(c *gin.Context) {
		adminId := mid.AdminId(c)
		adm := adc.GetReceiver().GetAdmin(adminId)
		if adm == nil {
			c.JSON(nhttp.StatusOK, model.Response{Code: response.ErrUnAuthorize})
			return
		}
		resp := model.Response{}
		var params wzabind.WzUserBetsReq
		if err := c.ShouldBind(&params); err != nil {
			logger.Error("err == ", zap.String("error", err.Error()))
			resp.Code = response.ErrBadRequest
			c.JSON(nhttp.StatusOK, resp)
			return
		}
		if params.Page <= 0 {
			params.Page = 1
		}
		if params.PageSize <= 0 {
			params.PageSize = 50
		}

		ret, code := stmsv.SV().GetUserBets(adm, params)
		resp.Code = code
		if code == 0 {
			resp.Data = gin.H{"data": ret}
		}

		c.JSON(nhttp.StatusOK, resp)
		return
	}
}

var userBetCountFn = func() (string, func(c *gin.Context)) {
	uri := fmt.Sprintf("/user/bets/count")
	return uri, func(c *gin.Context) {
		adminId := mid.AdminId(c)
		adm := adc.GetReceiver().GetAdmin(adminId)
		if adm == nil {
			c.JSON(nhttp.StatusOK, model.Response{Code: response.ErrUnAuthorize})
			return
		}
		resp := model.Response{}
		var params wzabind.WzUserBetsReq
		if err := c.ShouldBind(&params); err != nil {
			logger.Error("err == ", zap.String("error", err.Error()))
			resp.Code = response.ErrBadRequest
			c.JSON(nhttp.StatusOK, resp)
			return
		}
		if params.Page <= 0 {
			params.Page = 1
		}
		if params.PageSize <= 0 {
			params.PageSize = 50
		}

		ret, code := stmsv.SV().GetUserBetTotal(adm, params)
		resp.Code = code
		if code == 0 {
			resp.Data = gin.H{"total": ret}
		}

		c.JSON(nhttp.StatusOK, resp)
		return
	}
}

var userBetSumFn = func() (string, func(c *gin.Context)) {
	uri := fmt.Sprintf("/user/bets/sum")
	return uri, func(c *gin.Context) {
		adminId := mid.AdminId(c)
		adm := adc.GetReceiver().GetAdmin(adminId)
		if adm == nil {
			c.JSON(nhttp.StatusOK, model.Response{Code: response.ErrUnAuthorize})
			return
		}
		resp := model.Response{}
		var params wzabind.WzUserBetsReq
		if err := c.ShouldBind(&params); err != nil {
			logger.Error("err == ", zap.String("error", err.Error()))
			resp.Code = response.ErrBadRequest
			c.JSON(nhttp.StatusOK, resp)
			return
		}
		if params.Page <= 0 {
			params.Page = 1
		}
		if params.PageSize <= 0 {
			params.PageSize = 50
		}

		ret, code := stmsv.SV().GetUserBetSum(adm, params)
		resp.Code = code
		if code == 0 {
			resp.Data = gin.H{"data": ret}
		}

		c.JSON(nhttp.StatusOK, resp)
		return
	}
}
